package com.aitech.hardwareconctrl;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.friendlyarm.AndroidSDK.SomeUtils;

import java.util.ArrayList;
import java.util.List;

public class ArmActivity extends AppCompatActivity implements View.OnClickListener {

    public static EditText ang_L_1;
    public static EditText ang_L_2;
    public static EditText ang_L_3;
    public static EditText ang_L_4;
    public static EditText ang_L_5;
    public static EditText ang_H_1;
    public static EditText ang_H_2;
    public static EditText ang_R_1;
    public static EditText ang_R_2;
    public static EditText ang_R_3;
    public static EditText ang_R_4;
    public static EditText ang_R_5;

    public static EditText speed_L_1;
    public static EditText speed_L_2;
    public static EditText speed_L_3;
    public static EditText speed_L_4;
    public static EditText speed_L_5;
    public static EditText speed_H_1;
    public static EditText speed_H_2;
    public static EditText speed_R_1;
    public static EditText speed_R_2;
    public static EditText speed_R_3;
    public static EditText speed_R_4;
    public static EditText speed_R_5;

    public Button sendActionButton;
    public Button addActionGroupButton;
    public Button delActionGroupButton;
    public Button sendGroupDataButton;
    public Button viewGroupDataButton;
    public static String TAG = "ArmActivity";
    public static TextView texGroupViewText;
    public static List<int[]> list = new ArrayList<int[]>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_arm);
        initview();
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.sendAction) {
            sendAction();
        }
        if (id == R.id.addActionGroup) {
            addActionGroup();
        }
        if (id == R.id.dataGroupView) {
            showDataGroup();
        }
        if (id == R.id.delActionGroup) {
            delActionGroup();
        }
        if (id == R.id.sendGroupData) {
            sendGroupData();
        }

    }

    public void initview() {

        ang_L_1 = (EditText) findViewById(R.id.ang_L_1);
        ang_L_2 = (EditText) findViewById(R.id.ang_L_2);
        ang_L_3 = (EditText) findViewById(R.id.ang_L_3);
        ang_L_4 = (EditText) findViewById(R.id.ang_L_4);
        ang_L_5 = (EditText) findViewById(R.id.ang_L_5);
        ang_H_1 = (EditText) findViewById(R.id.ang_H_1);
        ang_H_2 = (EditText) findViewById(R.id.ang_H_2);
        ang_R_1 = (EditText) findViewById(R.id.ang_R_1);
        ang_R_2 = (EditText) findViewById(R.id.ang_R_2);
        ang_R_3 = (EditText) findViewById(R.id.ang_R_3);
        ang_R_4 = (EditText) findViewById(R.id.ang_R_4);
        ang_R_5 = (EditText) findViewById(R.id.ang_R_5);

        speed_L_1 = (EditText) findViewById(R.id.speed_L_1);
        speed_L_2 = (EditText) findViewById(R.id.speed_L_2);
        speed_L_3 = (EditText) findViewById(R.id.speed_L_3);
        speed_L_4 = (EditText) findViewById(R.id.speed_L_4);
        speed_L_5 = (EditText) findViewById(R.id.speed_L_5);
        speed_H_1 = (EditText) findViewById(R.id.speed_H_1);
        speed_H_2 = (EditText) findViewById(R.id.speed_H_2);
        speed_R_1 = (EditText) findViewById(R.id.speed_R_1);
        speed_R_2 = (EditText) findViewById(R.id.speed_R_2);
        speed_R_3 = (EditText) findViewById(R.id.speed_R_3);
        speed_R_4 = (EditText) findViewById(R.id.speed_R_4);
        speed_R_5 = (EditText) findViewById(R.id.speed_R_5);

        texGroupViewText = findViewById(R.id.GroupViewText);
        sendActionButton = (Button) findViewById(R.id.sendAction);
        addActionGroupButton = (Button) findViewById(R.id.addActionGroup);
        sendGroupDataButton = (Button) findViewById(R.id.sendGroupData);
        viewGroupDataButton = (Button) findViewById(R.id.dataGroupView);
        delActionGroupButton = (Button) findViewById(R.id.delActionGroup);
        sendActionButton.setOnClickListener(this);
        addActionGroupButton.setOnClickListener(this);
        delActionGroupButton.setOnClickListener(this);
        sendGroupDataButton.setOnClickListener(this);
        viewGroupDataButton.setOnClickListener(this);

    }

    public static int[] getdata() {
        String ang_L_1_angle = ang_L_1.getText().toString();
        int servo_L_1_angle = SomeUtils.getAngle(ang_L_1_angle);
        String ang_L_2_angle = ang_L_2.getText().toString();
        int servo_L_2_angle = SomeUtils.getAngle(ang_L_2_angle);
        String ang_L_3_angle = ang_L_3.getText().toString();
        int servo_L_3_angle = SomeUtils.getAngle(ang_L_3_angle);
        String ang_L_4_angle = ang_L_4.getText().toString();
        int servo_L_4_angle = SomeUtils.getAngle(ang_L_4_angle);
        String ang_L_5_angle = ang_L_5.getText().toString();
        int servo_L_5_angle = SomeUtils.getAngle(ang_L_5_angle);
        String ang_R_1_angle = ang_R_1.getText().toString();
        int servo_R_1_angle = SomeUtils.getAngle(ang_R_1_angle);
        String ang_R_2_angle = ang_R_2.getText().toString();
        int servo_R_2_angle = SomeUtils.getAngle(ang_R_2_angle);
        String ang_R_3_angle = ang_R_3.getText().toString();
        int servo_R_3_angle = SomeUtils.getAngle(ang_R_3_angle);
        String ang_R_4_angle = ang_R_4.getText().toString();
        int servo_R_4_angle = SomeUtils.getAngle(ang_R_4_angle);
        String ang_R_5_angle = ang_R_5.getText().toString();
        int servo_R_5_angle = SomeUtils.getAngle(ang_R_5_angle);

        String speed_L_1_angle = speed_L_1.getText().toString();
        int servo_L_1_speed = SomeUtils.getSpeed(speed_L_1_angle);
        String speed_L_2_angle = speed_L_2.getText().toString();
        int servo_L_2_speed = SomeUtils.getSpeed(speed_L_2_angle);
        String speed_L_3_angle = speed_L_3.getText().toString();
        int servo_L_3_speed = SomeUtils.getSpeed(speed_L_3_angle);
        String speed_L_4_angle = speed_L_4.getText().toString();
        int servo_L_4_speed = SomeUtils.getSpeed(speed_L_4_angle);
        String speed_L_5_angle = speed_L_5.getText().toString();
        int servo_L_5_speed = SomeUtils.getSpeed(speed_L_5_angle);
        String speed_R_1_angle = speed_R_1.getText().toString();
        int servo_R_1_speed = SomeUtils.getSpeed(speed_R_1_angle);
        String speed_R_2_angle = speed_R_2.getText().toString();
        int servo_R_2_speed = SomeUtils.getSpeed(speed_R_2_angle);
        String speed_R_3_angle = speed_R_3.getText().toString();
        int servo_R_3_speed = SomeUtils.getSpeed(speed_R_3_angle);
        String speed_R_4_angle = speed_R_4.getText().toString();
        int servo_R_4_speed = SomeUtils.getSpeed(speed_R_4_angle);
        String speed_R_5_angle = speed_R_5.getText().toString();
        int servo_R_5_speed = SomeUtils.getSpeed(speed_R_5_angle);

        String ang_H_1_angle = ang_H_1.getText().toString();
        int servo_H_1_angle = SomeUtils.getAngle(ang_H_1_angle);
        String ang_H_2_angle = ang_H_2.getText().toString();
        int servo_H_2_angle = SomeUtils.getAngle(ang_H_2_angle);

        String speed_H_1_angle = speed_H_1.getText().toString();
        int servo_H_1_speed = SomeUtils.getSpeed(speed_H_1_angle);
        String speed_H_2_angle = speed_H_2.getText().toString();
        int servo_H_2_speed = SomeUtils.getSpeed(speed_H_2_angle);

        String ang_H_1_angle_2 = SomeUtils.str2HexStr(ang_H_1_angle);
        int angint = Integer.parseInt(ang_H_2_angle);

        int[] getdata = {0xAA, 0x96, 0x03, 0x02, 0x00, 0x1c, 0x00, 0x01, 0x00, 0x01, servo_L_1_angle, servo_L_1_speed, servo_L_2_angle, servo_L_2_speed, servo_L_3_angle, servo_L_3_speed, servo_L_4_angle, servo_L_4_speed, servo_L_5_angle, servo_L_5_speed,
                servo_R_1_angle, servo_R_1_speed, servo_R_2_angle, servo_R_2_speed, servo_R_3_angle, servo_R_3_speed, servo_R_4_angle, servo_R_4_speed, servo_R_5_angle, servo_R_5_speed, servo_H_1_angle, servo_H_1_speed, servo_H_2_angle, servo_H_2_speed};
        Log.e(TAG, "---- getdata: ang_L_1_angle:" + ang_L_1_angle + " speed_L_1_angle:" + speed_L_1_angle + " speed_H_1_angle: " + speed_H_1_angle + " ang_H_2_angle:" + ang_H_2_angle + " ang_H_1_angle_2:" + ang_H_1_angle_2 + " angint:" + angint);
        return getdata;

    }

    public static void addDateGroup(int[] data) {

        if (list.size() > 0) {
            data[9] = list.size() + 1;
        }
        list.add(data);
        Log.e(TAG, "---- addDateGroup:listsize:" + list.size());
    }

    public static void showDataGroup() {

        if (texGroupViewText.getVisibility() == View.VISIBLE) {
            texGroupViewText.setVisibility(View.GONE);
        } else {
            texGroupViewText.setVisibility(View.VISIBLE);
        }
        String GroupDataText = "";
        for (int i = 0; i < list.size(); i++) {
            int[] data = list.get(i);
            int len = data.length;
            Log.e(TAG, "-----1   intArrayToByteArray  len = " + len);
            byte[] msgPack = new byte[len + 1];
            Log.e(TAG, "-----2 intArrayToByteArray  msgPack = " + msgPack);
            byte crc;
            for (int k = 0; k < len; k++) {
                msgPack[k] = SomeUtils.intToByte(data[k]);
                //Log.e(TAG, "-----3 intArrayToByteArray  a["+i+"]=" + a[i]+ "   msgPack["+i+"]=" + msgPack[i]);
            }
            GroupDataText = GroupDataText + SomeUtils.byte2HexStr(msgPack) + "\n";
            Log.e(TAG, "-----4  intArrayToByteArray  GroupDataText =" + GroupDataText);
        }
        texGroupViewText.setText(GroupDataText);
    }

    public static List<int[]> getlist() {
        List<int[]> listtemp = new ArrayList<int[]>();
        return list;
    }

    public static void sendAction() {
        SomeUtils.writeTodev2(getdata());
    }

    public static void addActionGroup() {
        int[] temp = getdata();
        addDateGroup(temp);
    }

    public static void delActionGroup() {
        if (list.size() != 0) {
            list.clear();
        }
    }

    public static void sendGroupData() {
        if (list.size() == 0) {
            SomeUtils.writeTodev2(getdata());
        } else {
            isActionDoingThread mstoopbowThread = new isActionDoingThread();
            mstoopbowThread.start();
        }
    }


    private static class isActionDoingThread extends Thread {
        public void run() {
            int i = 0;
            while (true) {
                try {
                    Thread.sleep(100);
                    i++;
                    if (i >= list.size()) {
                        SomeUtils.writeTodev2(list.get(i - 1));
                        Log.e(TAG, "");
                        break;
                    } else {
                        SomeUtils.writeTodev2(list.get(i - 1));
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}




